package org.longteng.mobileluck.algorithm.dao.impl;

import lombok.extern.slf4j.Slf4j;
import org.longteng.mobileluck.algorithm.bean.ChinesStrokesFortuneTable;
import org.longteng.mobileluck.algorithm.bean.GoodPlateNumTable;
import org.longteng.mobileluck.algorithm.dao.PlateNumberLuckDao;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 好车牌号表DAO实现
 */
@Slf4j
@Service
public class PlateNumberLuckDaoImpl extends ParserCsvBaseDao implements PlateNumberLuckDao {

    private final List<GoodPlateNumTable> goodPlateNumbers = new ArrayList<>();
    private  boolean csvInited = false;

    private void initReadCsv() {
        List<GoodPlateNumTable> goodPlateNumTableList = this.readTableCsv("/table/good_plate_number.csv");
        if (goodPlateNumTableList == null) {
            log.warn("读入csv【好车牌号表】。");
            goodPlateNumTableList = new ArrayList<>();
        }

        goodPlateNumbers.addAll(goodPlateNumTableList);
        csvInited = true;
        if (log.isDebugEnabled()) {
            log.debug("读入csv【好车牌号表】:\n ->{}", goodPlateNumbers);
        }
    }

    /**
     * 返回车牌好号3位清单
     *
     * @param prefix 号码组前缀，如2
     * @return
     */
    @Override
    public List<String> getGoodPlateNums(String prefix) {
        if( ! csvInited) {
            this.initReadCsv();
        }
        List<String> numList = new ArrayList<>();
        for (GoodPlateNumTable numEntity : goodPlateNumbers) {
            if(prefix == null || numEntity.startsWith(prefix)) {
                numList.add(numEntity.getGoodNum());
            }
        }

        return numList;
    }

    /**
     * 获取泛型T的类型 T.class
     * <br>Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
     * <br>可能会有错误：java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
     * <br>子类直接实现返回真实类class
     *
     * @return
     */
    @Override
    public Class getTClass() {
        return GoodPlateNumTable.class;
    }
}
